package org.apache.rocketmq.example.simple;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.example.MqConstants;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * 异步发送
 */
public class AsyncProducer {
    public static void main(
            String[] args) throws MQClientException, InterruptedException, UnsupportedEncodingException {

        DefaultMQProducer producer = new DefaultMQProducer("my_producer_group");
        producer.setNamesrvAddr(MqConstants.NameSrvArr);
        producer.start();
        producer.setRetryTimesWhenSendAsyncFailed(0);

        int messageCount = 10;
        final CountDownLatch countDownLatch = new CountDownLatch(messageCount);
        for (int i = 0; i < messageCount; i++) {
            try {
                final int index = i;
                Message msg = new Message("TopicTest",
                        "TagA",
                        "OrderID188",
                        "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
                producer.send(msg, new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        countDownLatch.countDown();
                        System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
                    }

                    @Override
                    public void onException(Throwable e) {
                        countDownLatch.countDown();
                        System.out.printf("%-10d Exception %s %n", index, e);
                        e.printStackTrace();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        countDownLatch.await(5, TimeUnit.SECONDS);
        producer.shutdown();
    }
}

class T {
    static int[] arr = new int[10];

    public static void main(String[] args) {
        System.out.println(arr[0]);
        System.out.println(arr[0]);
        System.out.println(arr[6]);
        System.out.println(arr[8]);

        String s = "abc";
        if (s == "abc") {
            System.out.println("true");
        } else {
            System.out.println("false");
        }


        int a = 9;
        System.out.println(a < 10 ? 99 : 9.9);
    }
}


class HelloA {
    public HelloA() {
        System.out.print("HelloA");
    }

    static {
        System.out.print("StaticA");
    }
}

class HelloB extends HelloA {
    public HelloB() {
        System.out.print("HelloB");
    }

    static {
        System.out.print("StaticB");
    }

    public static void main(String[] args) {
        new HelloB();
    }
}
